﻿using System;
using System.Net;
using System.Collections.Generic;

namespace Hydra.Shell.ProjectionAttributes
{
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public sealed class AssociationAttribute : Attribute
    {
        private bool isForeignKey;
        private string name;
        private string otherKey;
        private string thisKey;
        private bool isSimple;

        public AssociationAttribute()
        {
            isSimple = true;
        }

        public AssociationAttribute(string name, string thisKey, string otherKey)
        {
            this.name = name;
            this.thisKey = thisKey;
            this.otherKey = otherKey;
        }

        private static string[] GetKeyMembers(string key)
        {
            return key.Replace(" ", string.Empty).Split(new char[] { ',' });
        }

        public bool IsSimple
        {
            get
            {
                return isSimple;
            }
        }

        public bool IsForeignKey
        {
            get
            {
                return this.isForeignKey;
            }
            set
            {
                this.isForeignKey = value;
            }
        }

        public string Name
        {
            get
            {
                return this.name;
            }
        }

        public string OtherKey
        {
            get
            {
                return this.otherKey;
            }
        }

        public IEnumerable<string> OtherKeyMembers
        {
            get
            {
                return GetKeyMembers(this.OtherKey);
            }
        }

        public string ThisKey
        {
            get
            {
                return this.thisKey;
            }
        }

        public IEnumerable<string> ThisKeyMembers
        {
            get
            {
                return GetKeyMembers(this.ThisKey);
            }
        }
    }
}
